<?php

namespace app\api\controller\kuyou\applet;

use app\api\model\kuyou\applet\Acceptance as Production;
/**
 * 验收单接口
 */
class Acceptance extends Common
{
    protected $noNeedRight = 'getDetail';

    /**
     * 获取验收单
     * @ApiMethod (POST)
     * @param int $page 页码
     * @param int $limit 每页条数
     * @param string $type_name 分类名称
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?10:$limit;

            $purchase_code = $this->request->post('purchase_code');
            $where = [];
            if(!empty($purchase_code)){
                $where['purchase_code'] = ['like','%'.$purchase_code.'%'];
            }
            $status = $this->request->post('status');
            if($status!=null && $status!==''){
                $where['status'] = $status;
            }
            $examine_status = $this->request->post('examine_status');
            if($examine_status!=null && $examine_status!==''){
                $where['examine_status'] = $examine_status;
            }

            $result = Production::getList($where,$page,$limit);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 获取验收单详情
     * @ApiMethod (POST)
     * @param int $id
     */
    public function getdetail()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id');
            $result = Production::getDetail($id);
            if($result['status']==200){
                $this->success('查询成功',$result['msg'],200);
            }else{
                $this->error($result['msg'],'',$result['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 获取生产原料
     * @ApiMethod (POST)
     * @param int $id
     */
    public function pickinfo()
    {
        if ($this->request->isPost()) {
            $code_type = $this->request->post('code_type');
            $purchase_code = $this->request->post('purchase_code');
            $result = Production::pickinfo($code_type,$purchase_code);
            if($result['status']==200){
                $this->success('查询成功',$result['msg'],200);
            }else{
                $this->error($result['msg'],'',$result['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 添加同步商品
     * @ApiMethod (POST)
     * @param array $detail
     */
    public function acceptance()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            foreach ($data as $k=>$v){
                $data[$k] = is_array($v)?$v:html_entity_decode($v);
            }
            $ret = Production::acceptance($data);
            if($ret['status']==200){
                $this->success('操作成功',$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 添加验收单
     * @ApiMethod (POST)
     * @param array $detail
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            foreach ($data as $k=>$v){
                $data[$k] = is_array($v)?$v:html_entity_decode($v);
            }
            $ret = Production::addDetail($data);
            if($ret['status']==200){
                $this->success('添加成功',$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 更新验收单
     * @ApiMethod (POST)
     * @param array $detail
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            foreach ($data as $k=>$v){
                $data[$k] = is_array($v)?$v:html_entity_decode($v);
            }
            $ret = Production::editDetail($data);
            if($ret['status']==200){
                $this->success('更新成功','',200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 删除
     * @ApiMethod (POST)
     * @param int $id
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id');
            $ret = Production::delDetail($id);
            if($ret['status']==200){
                $this->success('删除成功',$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 审核
     * @ApiMethod (POST)
     * @param int $id
     */
    public function examine()
    {
        if ($this->request->isPost()) {
            $ids = $this->request->post('id');
            $type = $this->request->post('type');
            if($type=='examine'){
                $ret = Production::examine($ids);
                $msg = '审核成功';
            }else{
                $ret = Production::examinef($ids);
                $msg = '反审核成功';
            }
            if($ret['status']==1){
                $this->success($msg,$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

}
